package com.binvshe.binvshe.mine.corporation.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.RegisterResponce;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.MineInfoActivity;
import com.binvshe.binvshe.organization.entity.ClubEntity;
import com.binvshe.binvshe.organization.orgtype.SelectTypeFragment;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class CorpInfoFragment extends BaseFragment {

    private static final String KEY_CORP_NAME = "key_corp_name";
    private static final String KEY_CORP_PROVINCE = "key_corp_province";
    private static final String KEY_CORP_CITY = "key_corp_city";
    private static final String KEY_CLUB_HEAD = "key_club_head";
    private String mCorpName;
    private String mUrlClubHead;
    private int mCatId;
    private String mProvince;
    private String mCity;
    SelectTypeFragment mSelectTypeFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setArguments();
        initToolBar();
        initView();
        return view;
    }

    /**
     * @param corpName
     * @param province
     * @param city
     * @param clubHead
     * @return
     */
    public static CorpInfoFragment newInstance(String corpName, String province, String city, String clubHead) {

        Bundle args = new Bundle();
        args.putString(KEY_CORP_NAME, corpName);
        args.putString(KEY_CORP_PROVINCE, province);
        args.putString(KEY_CORP_CITY, city);
        args.putString(KEY_CLUB_HEAD, clubHead);
        CorpInfoFragment fragment = new CorpInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public CorpInfoFragment() {
    }


    private void initView() {
        mSelectTypeFragment = SelectTypeFragment.newInstance();
        FragmentUtils.replaceFragment(getChildFragmentManager(), R.id.frlytSelectType, mSelectTypeFragment);
    }

    private void setArguments() {
        mCorpName = getArguments().getString(KEY_CORP_NAME);
        mProvince = getArguments().getString(KEY_CORP_PROVINCE);
        mCity = getArguments().getString(KEY_CORP_CITY);
        mUrlClubHead = getArguments().getString(KEY_CLUB_HEAD);
    }

    private void initToolBar() {
        showToolbar();
        getCustomToolbar().setTitle("选择分类");
        getCustomToolbar().addRightButton("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCatId = mSelectTypeFragment.getSelectTypeID();
                Log.d("CorpInfoFragment", "mCatId:" + mCatId);
                if (mCatId == -1) {
                    EasyToast.showToast(getActivity(), "请选择社团分类");
                    return;
                }
                MyHttpRequest request = new MyHttpRequest(getContext());
                JSONObject jobj = new JSONObject();
                try {
                    jobj.put(ParamKey.Club.NAME, mCorpName);
                    jobj.put(ParamKey.PROVINCE, mProvince);
                    jobj.put(ParamKey.CITY, mCity);
                    jobj.put(ParamKey.PORTRAIT, mUrlClubHead);
                    jobj.put(ParamKey.Club.CATID, mCatId);
                    Log.e("ParamKey.Club.CATID",mCatId+"");

                    request.post(UrlCenter.CREAT_CLUB, jobj, new DataRequestListener<ClubEntity>(ClubEntity.class) {
                        @Override
                        public void onStart(Context context, String url) {
                            enableProgress(false);
                            super.onStart(context, url);
                        }

                        @Override
                        public void onSuccess(ClubEntity clubEntity) {
                            super.onSuccess(clubEntity);
                            AccountManager.getCurrentAccount().setInClub(true);
                            AccountManager.getCurrentAccount().setClub(clubEntity);
                            FragmentUtils.addFragment(getFragmentManager(), R.id.corpContainer, SubmitFragment.newInstance());
                        }

                        @Override
                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                            super.onFailure(t, errorNo, strMsg);
                            EasyToast.showToast(getContext(), strMsg);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        ImageButton backButton = getCustomToolbar().enableBackButton();
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtils.backStack(getActivity());
            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_corp_info, null);
    }
}
